package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.LeaderVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.CrudConstant;
import com.bbzn.device.client.constant.LeaderConstant;
import com.bbzn.device.client.constant.UserConstant;
import com.bbzn.device.client.dataobject.Leader;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.LeaderService;
import com.bbzn.device.client.service.PlanLeaderService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @Author wulongbo
 * @Date 2022/1/18 16:12
 * @Version 1.0
 */

@RestController
@RequestMapping("/leader")
@Slf4j
public class LeaderController extends BaseApiService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private LeaderService leaderService;

    @Autowired
    private PlanLeaderService planLeaderService;

    /**
     * 添加负责人
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addLeader", method = RequestMethod.POST)
    @OperationAnnotation(content = "添加负责人")
    public BaseResponse addLeader(@RequestBody LeaderVO leaderVO, HttpServletRequest request) throws Exception {
        Long userId = commonService.getUserId(request);
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        String username = MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(leaderVO.getName()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_NAME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(leaderVO.getPhone()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(UserConstant.USER_ENUM, UserConstant.PHO_UN_PHONE_PREFIX + language);
            throw new DefinitException(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(leaderVO.getPostId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_POST_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(leaderVO.getAge()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_AGE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(leaderVO.getSex()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_SEX_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(leaderVO.getTitle()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_TITLE_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        if (!OptionalBean.ofNullable(leaderVO.getImg()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(UserConstant.USER_ENUM, UserConstant.USER_UN_IMG_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }

        if (!PhoneFormatCheckUtils.isPhoneLegal(leaderVO.getPhone())) {
            SystemParam systemParam = systemParamByCacheService.getValues(UserConstant.USER_ENUM, UserConstant.PHO_NUM_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }
        // 手机号 不得重复
        Leader query = new Leader();
        query.setPhone(leaderVO.getPhone());
        int result = leaderService.selectCountBySelective(query);
        if (result > 0) {
            SystemParam systemParam = systemParamByCacheService.getValues(UserConstant.USER_ENUM, UserConstant.PHO_PWD_NOT_NATCH_PREFIX + language);
            return setResultError(systemParam.getCode(), systemParam.getMassage());
        }

        Leader insert = BabaBeanUtils.voToDto(leaderVO, Leader.class);
        insert.setUserId(userId);
        insert.setCreateTime(new Date());
        insert.setCreatedBy(username);
        leaderService.insertSelective(insert);
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.ADD_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }


    /**
     * 查询负责人
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getLeaderList", method = RequestMethod.POST)
    @OperationAnnotation(content = "查询负责人")
    public BaseResponse getLeaderList(HttpServletRequest request) {
        Long userId = commonService.getUserId(request);
        Leader query = new Leader();
        query.setUserId(userId);
        List<Leader> leaders = leaderService.findByAll(query);
        return setResultSuccess(leaders);
    }

    /**
     * 修改负责人
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateLeader", method = RequestMethod.POST)
    @OperationAnnotation(content = "修改负责人")
    public BaseResponse updateLeader(@RequestBody LeaderVO leaderVO, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        String username = MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(leaderVO.getId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        Leader update = BabaBeanUtils.voToDto(leaderVO, Leader.class);
        update.setUpdateTime(new Date());
        update.setUpdatedBy(username);
        leaderService.updateByPrimaryKeySelective(update);
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 删除负责人
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delLeader", method = RequestMethod.POST)
    @OperationAnnotation(content = "删除负责人")
    public BaseResponse delLeader(@RequestBody LeaderVO leaderVO, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (!OptionalBean.ofNullable(leaderVO.getId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(LeaderConstant.LEADER_ENUM, LeaderConstant.LEADER_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        leaderService.deleteByPrimaryKey(leaderVO.getId());
        planLeaderService.deleteByLeaderId(leaderVO.getId());
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.DELETE_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

}
